package com.willpower.eureka.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willpower.eureka.entity.SysUser;
import com.willpower.eureka.mapper.SysUserMapper;
import com.willpower.eureka.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wen-yi
 * @since 2021-11-05
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public JSONObject selectPage(String query, Integer pageNum, Integer pageSize) {
        JSONObject json = new JSONObject();
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(query)){
            wrapper.like(SysUser::getId, query).or().like(SysUser::getUsername, query)
                    .or().like(SysUser::getNickname,query).or().like(SysUser::getPhoneNumber,query);
        }
        Page<SysUser> page = sysUserMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        List<SysUser> records = page.getRecords();
        long total = page.getTotal();

        json.put("status", true);
        json.put("code", 200);
        json.put("msg", "成功");
        json.put("data", records);
        json.put("pageNum", pageNum);
        json.put("total", total);
        return json;
    }

    @Override
    public int changeUserStatus(Integer id, Boolean enabled) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getId,id);
        SysUser sysUser = new SysUser();
        sysUser.setEnabled(String.valueOf(enabled));
        return sysUserMapper.update(sysUser, wrapper);
    }
}
